//
//  FlashLight.m
//  gdemm
//
//  Created by 乔明超 on 16/5/10.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "FlashLight.h"

@implementation FlashLight

static FlashLight *mInStance;

+ (instancetype)getInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mInStance = [[self alloc]init];
    });
    
    return mInStance;
}

- (void)setOnOffFlag:(BOOL)onOffFlag{
    _onOffFlag = onOffFlag;
    [self onOffHandler];
}
- (AVCaptureDevice *)myDevice{
    if (_myDevice == nil) {
        _myDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    }
    
    return _myDevice;
}

- (void)setTorchOn{
    [self.myDevice lockForConfiguration:nil];
    if ([self.myDevice hasTorch]) {
        [self.myDevice setTorchMode:AVCaptureTorchModeOn];
        
    }
    [self.myDevice unlockForConfiguration];
    
}

- (void)setTorchOff{
    [self.myDevice lockForConfiguration:nil];
    if ([self.myDevice hasTorch]) {
        [self.myDevice setTorchMode:AVCaptureTorchModeOff];
    }
    [self.myDevice unlockForConfiguration];
    
    
}
- (void)onOffHandler{

    if (self.onOffFlag == YES) {
        [self setTorchOn];
    }else{
        [self setTorchOff];
    }
}
@end
